<template>
  <div class="app-container">
    <!--查询-->
    <div class="my-bar">
      <el-button-group class="float-left">
        <el-button size="small" type="primary" icon="el-icon-edit" @click="onEdit()">新增</el-button>
      </el-button-group>
      <el-form :inline="true" class="float-right" size="small">
        <el-form-item v-if="isAdmin">
          <el-input v-model="getData.params.id" placeholder="请输入ID" clearable />
        </el-form-item>
        <!--自动生成搜索代码开始-->
        <el-form-item>
          <el-input v-model="getData.params.name" placeholder="请输入项目名" clearable />
        </el-form-item>
        <!--自动生成搜索代码结束-->
        <el-form-item>
          <el-button type="primary" @click="func.getListData($data)"><i class="el-icon-search" /></el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--展示表格-->
    <el-table v-loading="listData.loading" :data="listData.data" element-loading-text="载入中..." border fit highlight-current-row>
      <el-table-column :show-overflow-tooltip="true" align="center" prop="id" label="自增ID" width="70" />
      <!--自动生成表格代码开始-->
      <el-table-column align="center" label="项目" width="130px">
        <template slot-scope="scope">
          项目名: {{ scope.row.name }}<br>
          <el-tag v-if="scope.row.type == 1" size="mini">simple</el-tag>
          <el-tag v-if="scope.row.type == 2" size="mini">sping-cloud</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="公司和作者" width="160px">
        <template slot-scope="scope">
          公司: {{ scope.row.company }}<br>
          作者: {{ scope.row.author }}<br>
          说明: {{ scope.row.cmmt }}
        </template>
      </el-table-column>
      <el-table-column type="expand" label="模块" width="60px">
        <template slot-scope="props">
          <el-table :data="props.row.moduleList" border fit highlight-current-row>
            <el-table-column align="center" prop="name" label="模块名" />
            <el-table-column align="center" prop="cmmt" label="模块注释" />
          </el-table>
        </template>
      </el-table-column>
      <el-table-column align="center" label="模板" width="255px">
        <template slot-scope="scope">
          项目: {{ scope.row.tplProject }}  模块: {{ scope.row.tplModule }}  类名: {{ scope.row.tplClass }}<br>
          客户层: {{ scope.row.tplRest }}<br>
          中间层: {{ scope.row.tplFacade }}<br>
          <!-- 服务层: {{ scope.row.tplService }}<br> -->
          <!-- 地址: {{ scope.row.tplPath }} -->
        </template>
      </el-table-column>
      <el-table-column label="前端和目标" min-width="255px">
        <template slot-scope="scope">
          代码地址: {{ scope.row.dstPath }}<br>
          前端地址: {{ scope.row.vuePath }}<br>
          <!-- 前段模板: {{ scope.row.vueTpl }} -->
        </template>
      </el-table-column>
      <!--自动生成表格代码结束-->
      <el-table-column prop="operation" fixed="right" width="172px" label="操作" align="center">
        <template slot-scope="scope">
          <!-- 同步时间：<br>
          {{ scope.row.flushTime | parseTime() }}<br> -->
          <el-button :disabled="loadings[scope.$index]" size="mini" type="primary" @click="onEdit(scope)">编辑</el-button>
          <el-dropdown size="mini">
            <el-button :loading="loadings[scope.$index]" size="mini" type="primary">更多 <i class="el-icon-arrow-down" /></el-button>
            <el-dropdown-menu slot="dropdown">
              <!-- <el-dropdown-item divided @click.native="onCommand(scope, '初始化')">初始化</el-dropdown-item>
              <el-dropdown-item divided @click.native="onCommand(scope, '更新SQL')">更新SQL</el-dropdown-item> -->
              <el-dropdown-item divided @click.native="onCommand(scope, '拷贝创建')">拷贝创建</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <div class="my-bar">
      <el-pagination :page-size="getData.pageSize" :total="listData.total" class="float-right" layout="total, prev, pager, next" @current-change="onPageNum" />
    </div>
    <!--新增和编辑-->
    <el-dialog :title="popData.title" :visible.sync="popData.show" width="80%">
      <el-form ref="formData" :model="formData" :rules="formRules" label-width="120px" size="small">
        <!--自动生成编辑代码开始-->
        <el-row>
          <el-col :span="8">
            <el-form-item label="项目名" required>
              <el-input v-model="formData.name" placeholder="请输入项目名" maxlength="10" show-word-limit clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目类型" required>
              <el-radio-group v-model="formData.type">
                <el-radio-button :label="1">simple</el-radio-button>
                <el-radio-button :label="2">spring-cloud</el-radio-button>
                <el-radio-button :label="3">spring-dubbo</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="项目作者" required>
              <el-input v-model="formData.author" placeholder="请输入项目作者" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="12">
            <el-form-item label="项目注释" required>
              <el-input v-model="formData.cmmt" placeholder="请输入项目注释" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12">
            <el-form-item label="项目公司" required>
              <el-input v-model="formData.company" placeholder="请输入项目公司" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :xs="24" :sm="12">
            <el-form-item label="模版客户层" required>
              <el-input v-model="formData.tplRest" placeholder="请输入模版客户层" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12">
            <el-form-item label="模版中间层" required>
              <el-input v-model="formData.tplFacade" placeholder="请输入模版中间层" clearable />
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>
          <!-- <el-col :xs="24" :sm="12">
            <el-form-item label="模板地址" required>
              <el-input v-model="formData.tplPath" placeholder="请输入模板地址" clearable />
            </el-form-item>
          </el-col> -->
          <el-col :xs="24" :sm="12">
            <el-form-item label="代码地址" required>
              <el-input v-model="formData.dstPath" placeholder="请输入代码地址" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12">
            <el-form-item label="前端地址" required>
              <el-input v-model="formData.vuePath" placeholder="请输入前端地址" clearable />
            </el-form-item>
          </el-col>
        </el-row>
        <!--自动生成编辑代码结束-->
        <el-tabs size="mini">
          <el-tab-pane label="模块">
            <el-button-group style="margin-bottom:10px">
              <el-button size="small" type="primary" icon="el-icon-edit" @click="func.onAdd(formData.moduleList)">新增</el-button>
            </el-button-group>
            <!--字段基础属性-->
            <el-table v-loading="listData.loading" :data="formData.moduleList" element-loading-text="载入中..." size="mini" border fit highlight-current-row>
              <!-- <el-table-column :show-overflow-tooltip="true" align="center" prop="id" label="UUID" /> -->
              <el-table-column label="名字">
                <template slot-scope="scope">
                  <el-form-item :rules="[{ required: true, message: '模块名不能为空'},{ min: 2, max: 16, message: '长度在 2 到 16 个字符', trigger: 'blur' }]" :prop="`moduleList[`+scope.$index+`].name`" label-width="0" style="margin-top:15px">
                    <el-input v-model="scope.row.name" placeholder="英文小写(bas,sys,pay)" clearable />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="文件夹">
                <template slot-scope="scope">
                  <el-form-item :rules="[{ required: true, message: '文件夹不能为空'},{ min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }]" :prop="`moduleList[`+scope.$index+`].folder`" label-width="0" style="margin-top:15px">
                    <el-input v-model="scope.row.folder" placeholder="英文小写(code-gen,jjcm)" clearable />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="注释">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.cmmt" size="small" clearable />
                </template>
              </el-table-column>
              <el-table-column label="操作" width="65px">
                <template slot-scope="scope">
                  <el-button :disabled="scope.row.disable" type="danger" size="mini" @click="func.onDel(formData.moduleList, scope.$index)"><i class="el-icon-close" /></el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="popData.show = false; popData.loading = false">取消</el-button>
        <el-button :loading="popData.loading" size="small" type="primary" @click="func.saveEvts(_self)">{{ popData.button }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import api from '@/api/service'
import { isAdmin } from '@/api/service'
import mySelect from '@/views/compts/input'

export default {
  components: {
    mySelect
  },
  data() {
    return {
      func: api,
      bean: 'tpl.project',
      isAdmin: isAdmin(),
      popData: {},
      getData: {
        pageNum: 1,
        pageSize: 10,
        showFlags: 'moduleList[id,name,cmmt,folder]',
        params: {
        }
      },
      listData: {
        data: [],
        total: 0,
        loading: false
      },
      showData: {
        loading: false,
        show: false,
        sql: ''
      },
      formRules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ]
      },
      formData: {},
      editForm: {
      },
      loadings: [],
    }
  },
  mounted() {
    api.getListData(this.$data)
  },
  methods: {
    onPageNum(val) {
      this.getData.pageNum = val
      api.getListData(this.$data)
    },
    onCommand(scope, command) {
      console.log(command, scope)
      if (command === '删除') {
        api.cmStatus(this, scope, 0, command)
        // } else if (command === '初始化') {
        //   this.$set(this.loadings, scope.$index, true)
        //   api.newRequest('project', 'init', { id: scope.row.id }).then(res => {
        //     console.log(res)
        //     this.$set(this.loadings, scope.$index, false)
        //     this.$message({
        //       type: 'success',
        //       message: '已经初始化完毕!'
        //     })
        //   })
      } else if (command === '拷贝创建') {
        this.popData.show = true
        this.popData.loading = false
        this.popData.button = '拷贝新增'
        this.popData.title = '拷贝新增'
        this.formData = JSON.parse(JSON.stringify(scope.row))
        delete this.formData.id
        for (const module of this.formData.moduleList) {
          delete module.id
        }
      }
    },
    onEdit(scope) {
      api.onEdit(this.$data, scope)
      if (!this.formData.moduleList) {
        this.formData.moduleList = []
      }
    },
  }
}
</script>
